#!/usr/bin/env python
#coding: utf-8
'''
Created on 2017年3月3日

@author: Administrator
'''
from common.decorators import login_required
from django.template.response import TemplateResponse
from ioffice import settings
from ioffice.utils import usersource
from funcresource.models import UserResource
import copy

@login_required
def index(request, template="ioffice/admin/base.html"):
    rsp_data = copy.copy(settings.ERROR["SUCC"])
    rsp_data["data"] = {}
    rsp_data["data"]["usources"] = []
    usources = UserResource.objects.filter(user=request.user, resource__state=settings.StateEnum.VALID, resource__parent=None, state=settings.StateEnum.VALID)
    for usource in usources:
        record = {
            "name":usource.resource.name,
            "url":usource.resource.url,
            "childrents":[],
        }
        childrents = usersource.get_childrent_source(request.user, usource.resource)
        if childrents:
            record["childrents"] = childrents
            rsp_data["data"]["usources"].append(record)
    return TemplateResponse(request, template, rsp_data)
    
    
